package sg.edu.nus.iss.ooad.jass.controller;

import java.awt.Frame;
import java.util.logging.Level;
import java.util.logging.Logger;
import sg.edu.nus.iss.ooad.jass.entity.Advertiser;
import sg.edu.nus.iss.ooad.jass.entity.JobAdvertisement;
import sg.edu.nus.iss.ooad.jass.entity.Jobseeker;
import sg.edu.nus.iss.ooad.jass.entity.Response;
import sg.edu.nus.iss.ooad.jass.ui.AdvertiserCreateJobAdvertisementForm;
import sg.edu.nus.iss.ooad.jass.ui.AdvertiserListCompanyForm;
import sg.edu.nus.iss.ooad.jass.ui.AdvertiserListResponseForm;
import sg.edu.nus.iss.ooad.jass.ui.AdvertiserUpdateJobAdvertisementForm;
import sg.edu.nus.iss.ooad.jass.ui.JASSAdvertisementDetailForm;
import sg.edu.nus.iss.ooad.jass.ui.JASSAppFrame;
import sg.edu.nus.iss.ooad.jass.ui.JASSCreateAdvertiserForm;
import sg.edu.nus.iss.ooad.jass.ui.JASSJobSeekerProfileForm;
import sg.edu.nus.iss.ooad.jass.ui.JASSJobSeekerRegistrationForm;
import sg.edu.nus.iss.ooad.jass.ui.JASSJobSeekerUpdateProfileForm;
import sg.edu.nus.iss.ooad.jass.ui.JASSLoginForm;
import sg.edu.nus.iss.ooad.jass.ui.JASSResponseConfirmation;
import sg.edu.nus.iss.ooad.jass.ui.JASSUpdateAdvertiserForm;
import sg.edu.nus.iss.ooad.jass.ui.widget.AbstractJASSRegularWindow;

/**
 *
 * Main Controller. This is the main controller for JASS.
 *
 * @author Isak Rabin (HT082193M)
 * @version 1.0
 * @since 1.0
 *
 * Development Environment : Netbeans IDE v6.7
 * Name of the File        : MainController.java
 * Creation / Modification History
 *         Isak Rabin        20-Sep-2009        Created
 *
 */
public class MainController extends Thread implements Runnable {

        private final static Logger logger = Logger.getLogger(MainController.class.getName());
        //These variables are belong to Singleton objects
        private static MainController instance = null;
        //Form
        public final static int ADVERTISER_CREATE_JOBADVERTISEMENT_FORM = 1;
        public final static int ADVERTISER_LIST_COMPANY_FORM = 2;
        public final static int ADVERTISER_LIST_JOBADVERTISEMENT_FORM = 3;
        public final static int ADVERTISER_LIST_RESPONSE_FORM = 4;
        public final static int ADVERTISER_UPDATE_JOBADVERTISEMENT_FORM = 5;
        public final static int JASS_ADVERTISEMENT_DETAILS_FORM = 6;
        public final static int JASS_CREATE_ADVERTISER_FORM = 7;
        public final static int JASS_CREATE_JOBSEEKER_FORM = 8;
        public final static int JASS_LOGIN_FORM = 9;
        public final static int JASS_RESPONSE_CONFIRMATION = 10;
        public final static int JASS_SEARCH_JOBADVERTISEMENT_FORM = 11;
        public final static int JASS_UPDATE_ADVERTISER_FORM = 12;
        public final static int JASS_UPDATE_JOBSEEKER_FORM = 13;
        public final static int JASS_SHOW_MSG = 14;
        public final static int JASS_JOB_SEEKER_PROFILE_FORM = 15;
        //Flag if the configuration has been loaded
        private boolean loaded = false;
        private Jobseeker jobSeeker;
        private Advertiser advertiser;
        private JobAdvertisement jobAdvertisement;
        private Response response;
        private boolean editable;
        private String msg;
        private Frame Frameowner;

        /**
         * MainController constructor
         */
        private MainController() {
                start();
        }

        /**
         * Singleton method get MainController instance.
         * @return MainController instance
         */
        public static MainController getInstance() {
                if (instance == null) {
                        instance = new MainController();
                }
                return instance;
        }

        /**
         *  Start application
         */
        @Override
        public void start() {
                logger.info("Starting JASS Application");
                if (!loaded) {
                        SystemSettingController.loadData();
                        loaded = true;
                        gotoScreen(JASS_SEARCH_JOBADVERTISEMENT_FORM, null);
                }
                logger.info("JASS Application started");
        }

        /**
         * This operation will show application main window.
         */
        public void showAppFrame() {
                JASSAppFrame apps = new JASSAppFrame(this);
                apps.setVisible(true);

        }

        /**
         * This operation will show login form.
         * @param parent Parent window for Login Form.
         */
        public void showLoginForm(AbstractJASSRegularWindow parent) {
                JASSLoginForm scr = JASSLoginForm.getInstance(parent);
                scr.setVisible(true);
                scr.setLocationRelativeTo(parent);
                scr.setFocusable(true);
        }

        /**
         * This operation will show Update Job Advertisement screen for Advertiser.
         * @param parent Parent window for AdvertiserUpdateJobAdvertisement Form.
         */
        public void showAdvertiserUpdateJobAdvertisementForm(AbstractJASSRegularWindow parent, JobAdvertisement jobadAdvertisement) {
               AdvertiserUpdateJobAdvertisementForm scr = AdvertiserUpdateJobAdvertisementForm.getInstance(parent);
               scr.setJobAdvertisement(jobAdvertisement);
               scr.setVisible(true);
               scr.setLocationRelativeTo(parent);
               scr.setFocusable(true);
        }

        /**
         * This method is to show Advertiser Main Screen()
         */
        public void showJASSUpdateAdvertiserForm(AbstractJASSRegularWindow parent) {
                JASSUpdateAdvertiserForm advertiserScreen = new JASSUpdateAdvertiserForm( parent);
                try {
                        advertiserScreen.setAdvertiser(getAdvertiser());
                        advertiserScreen.display();
                        advertiserScreen.setVisible(true);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                }
        }

        /**
         * 
         */
        public void showJASSCreateAdvertiserForm(AbstractJASSRegularWindow parent) {
                JASSCreateAdvertiserForm advertiserScreen = new JASSCreateAdvertiserForm(parent);
                advertiserScreen.setVisible(true);
        }

        /**
         * 
         */
        public void ShowJASSAdvertisementDetailForm() {
                JASSAdvertisementDetailForm scr = new JASSAdvertisementDetailForm();
                scr.setJobadvertisement(getJobAdvertisement());

                scr.display();
                scr.setLocation(200, 0);
                scr.setVisible(true);
                scr.setFocusable(true);
        }

        /**
         *
         * @param job response
         */
        public void showAdvertiserListResponseForm(AbstractJASSRegularWindow parent ,JobAdvertisement job) {

                AdvertiserListResponseForm responseForm = new AdvertiserListResponseForm(parent,job);
                responseForm.setVisible(true);

        }

        /**
         * 
         */
        public void showCompanyListForm(AbstractJASSRegularWindow parent) {
                AdvertiserListCompanyForm companyForm;
                if (parent instanceof AdvertiserUpdateJobAdvertisementForm) {
                        companyForm = AdvertiserListCompanyForm.getInstance(AdvertiserUpdateJobAdvertisementForm.getInstance(parent));
                } else {
                        companyForm = AdvertiserListCompanyForm.getInstance(AdvertiserCreateJobAdvertisementForm.getInstance(parent));
                }
                companyForm.setVisible(true);

        }

        /**
         *
         */
        public void showCreateJobseekerScreen() {
                JASSJobSeekerRegistrationForm jobSeekerScreen = new JASSJobSeekerRegistrationForm();
                jobSeekerScreen.setVisible(true);
        }

        /**
         *
         * @param jobseeker
         */
        public void showUpdateJobseekerScreen(AbstractJASSRegularWindow parent ,Jobseeker jobseeker) {
                JASSJobSeekerUpdateProfileForm jobSeekerScreen = new JASSJobSeekerUpdateProfileForm(parent, jobseeker);
                jobSeekerScreen.setEditable(isEditable());
                jobSeekerScreen.display(jobseeker);
                jobSeekerScreen.setVisible(true);
        }

        /**
         *
         */
        public void showAdvertiserCreateJobAdvertisementForm(AbstractJASSRegularWindow parent) {
                AdvertiserCreateJobAdvertisementForm scr = AdvertiserCreateJobAdvertisementForm.getInstance(parent);
                scr.setLocationRelativeTo(parent);
                scr.setVisible(true);
                scr.setFocusable(true);
        }

        /**
         *
         * @param jobseeker
         */
        public void showJASSJobSeekerProfileForm(Jobseeker jobseeker) {
                JASSJobSeekerProfileForm jobSeekerScreen = new JASSJobSeekerProfileForm(jobseeker);
                jobSeekerScreen.display(jobseeker);
                jobSeekerScreen.setVisible(true);
        }

        /**
         *
         * @param response
         */
        public void showResponseConfirmationScreen(AbstractJASSRegularWindow parent ,Response response) {
                JASSResponseConfirmation confirmation = new JASSResponseConfirmation(parent);
                confirmation.setResponse(response);
                confirmation.display();
                confirmation.setVisible(true);
        }

        public Frame getFrameowner() {
                return Frameowner;
        }

        public void setFrameowner(Frame Frameowner) {
                this.Frameowner = Frameowner;
        }

        public String getMsg() {
                return msg;
        }

        public void setMsg(String msg) {
                this.msg = msg;
        }

        public boolean isEditable() {
                return editable;
        }

        public Response getResponse() {
                return response;
        }

        public void setResponse(Response response) {
                this.response = response;
        }

        public void setEditable(boolean editable) {
                this.editable = editable;
        }

        public Advertiser getAdvertiser() {
                return advertiser;
        }

        public void setAdvertiser(Advertiser advertiser) {
                this.advertiser = advertiser;
        }

        public JobAdvertisement getJobAdvertisement() {
                return jobAdvertisement;
        }

        public void setJobAdvertisement(JobAdvertisement jobAdvertisement) {
                this.jobAdvertisement = jobAdvertisement;
        }

        public Jobseeker getJobSeeker() {
                return jobSeeker;
        }

        public void setJobSeeker(Jobseeker jobSeeker) {
                this.jobSeeker = jobSeeker;
        }

        /**
         * Change the screen display
         * @param screen
         */
        public void gotoScreen(int screen, AbstractJASSRegularWindow parent) {
                try {
                        switch (screen) {
                                case ADVERTISER_CREATE_JOBADVERTISEMENT_FORM:
                                        showAdvertiserCreateJobAdvertisementForm(parent);
                                        break;

//                                case ADVERTISER_UPDATE_JOBADVERTISEMENT_FORM:
//                                        showAdvertiserUpdateJobAdvertisementForm(parent);
//                                        break;

                                case JASS_LOGIN_FORM:
                                        showLoginForm(parent);
                                        break;

                                case JASS_UPDATE_ADVERTISER_FORM:
                                        showJASSUpdateAdvertiserForm(  parent);
                                        break;

                                case ADVERTISER_LIST_RESPONSE_FORM:
                                        showAdvertiserListResponseForm(parent, getJobAdvertisement());
                                        break;

                                case ADVERTISER_LIST_COMPANY_FORM:
                                        showCompanyListForm(parent);
                                        break;

                                case JASS_UPDATE_JOBSEEKER_FORM:
                                        showUpdateJobseekerScreen(  parent ,getJobSeeker());
                                        break;

                                case JASS_CREATE_JOBSEEKER_FORM:
                                        showCreateJobseekerScreen();
                                        break;

                                case JASS_ADVERTISEMENT_DETAILS_FORM:
                                        ShowJASSAdvertisementDetailForm();
                                        break;

                                case JASS_CREATE_ADVERTISER_FORM:
                                        showJASSCreateAdvertiserForm(  parent);
                                        break;
                                case JASS_RESPONSE_CONFIRMATION:
                                        showResponseConfirmationScreen(  parent ,getResponse());
                                        break;
                                case JASS_SEARCH_JOBADVERTISEMENT_FORM:
                                        showAppFrame();
                                        break;

                                case JASS_JOB_SEEKER_PROFILE_FORM:
                                    showJASSJobSeekerProfileForm(  getJobSeeker()) ;
                                        
                        }
                } catch (Exception e) {
                        logger.log(Level.SEVERE, e.getMessage(), e);
                        e.printStackTrace();
                }
        }

}
